package com.zty.xiaomi.server.Controller;


import com.sun.jmx.snmp.Timestamp;
import com.zty.xiaomi.server.Dto.GetInsuranceInfoDto;
import com.zty.xiaomi.server.Entity.Insurance.Insurance;
import com.zty.xiaomi.server.Service.Insurance.InsuranceServiceImp;
import com.zty.xiaomi.server.Service.Order.OrderServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("insurance")
public class InsuranceController {

    @Autowired
    private InsuranceServiceImp insuranceServiceImp;

    @Autowired
    private OrderServiceImp orderServiceImp;

    @GetMapping("getInsuranceByCategoryId")
    private GetInsuranceInfoDto getInsuranceByCategoryId(@RequestParam("category_id") int category_id) throws IOException {
        GetInsuranceInfoDto getInsuranceInfoDto = new GetInsuranceInfoDto();
        List<Insurance> list = insuranceServiceImp.getInsuranceByCategoryId(category_id);
        if (list.size() == 0) {
            getInsuranceInfoDto.setStatus("400");
            getInsuranceInfoDto.setMsg("此分类暂无可选保险");
        } else {
            getInsuranceInfoDto.setList(insuranceServiceImp.getInsuranceByCategoryId(category_id));
            getInsuranceInfoDto.setStatus("200");
        }

        return getInsuranceInfoDto;
    }

    @GetMapping("getInsuranceByInsuranceId")
    private GetInsuranceInfoDto getInsuranceByInsuranceId(@RequestParam("insuranceIdArr") int[] insuranceIdArr) throws IOException {
        GetInsuranceInfoDto getInsuranceInfoDto = new GetInsuranceInfoDto();
        List<Insurance> list = new ArrayList<Insurance>();
        int count = 0;
        for (int i = 0; i < insuranceIdArr.length; i++) {
            Insurance insurance = insuranceServiceImp.getInsuranceByInsuranceId(insuranceIdArr[i]);
            if (insurance != null) {
                count++;
                list.add(insurance);
            }
        }
        getInsuranceInfoDto.setList(list);
        if (count != insuranceIdArr.length) {
            getInsuranceInfoDto.setStatus("400");
            getInsuranceInfoDto.setMsg("查询数据量与预期有差,实际数据为" + count + "条");
        }
        getInsuranceInfoDto.setStatus("200");
        return getInsuranceInfoDto;
    }

    @GetMapping("getInsuranceByOrderNoAndGoodId")
    private GetInsuranceInfoDto getInsuranceByOrderNoAndGoodId(@RequestParam("orderNo") int orderNo,
                                                               @RequestParam("good_id") int good_id) throws IOException, ParseException {
        GetInsuranceInfoDto getInsuranceInfoDto = new GetInsuranceInfoDto();
        List<Insurance> list = new ArrayList<Insurance>();
        String insuranceIdStr = orderServiceImp.getInsuranceIdByOrderNoAndGoodId(orderNo, good_id);
        if (insuranceIdStr != null) {
            String[] insuranceIdArr = insuranceIdStr.split(",");
            String s = orderServiceImp.getpaymentTimeByOrderNo(orderNo);
            long paymentTime = Long.valueOf(dateToStamp(s));
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            long time = System.currentTimeMillis();
            int count = 0;
            for (int i = 0; i < insuranceIdArr.length; i++) {
                Insurance insurance = insuranceServiceImp.getInsuranceByInsuranceId(Integer.parseInt(insuranceIdArr[i]));
                long sum = paymentTime + insurance.getTime() * 1000;
                if (sum - time > 0) {
                    list.add(insurance);
                    count++;
                }
            }

            if (count == insuranceIdArr.length) {
                getInsuranceInfoDto.setList(list);
                getInsuranceInfoDto.setStatus("200");
                getInsuranceInfoDto.setMsg("保险信息获取成功");
            } else {
                int num = insuranceIdArr.length-count;
                getInsuranceInfoDto.setList(list);
                getInsuranceInfoDto.setStatus("400");
                getInsuranceInfoDto.setMsg("该商品保险已过期：" + num+ "个" );
            }

            getInsuranceInfoDto.setList(list);
            getInsuranceInfoDto.setStatus("200");
        } else {
            getInsuranceInfoDto.setStatus("400");
            getInsuranceInfoDto.setMsg("此商品无保险");
        }
        return getInsuranceInfoDto;
    }

    /*
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }
}
